package com.example.demo.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * Description:
 * User: 故我
 * Date: 2023-11-05
 * Time: 21:38
 */
@ControllerAdvice
@ResponseBody
public class MyExceptionHandler {
    //空指针异常的统一处理
    @ExceptionHandler(NullPointerException.class)
    public HashMap<String,Object> nullPointException(NullPointerException e){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("code",-1);
        hashMap.put("msg 空指针异常信息：",e.getMessage());
        hashMap.put("data",null);
        return hashMap;
    }

    //所有异常的统一处理
    @ExceptionHandler(Exception.class)
    public HashMap<String,Object> exception(Exception e){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("code",-1);
        hashMap.put("msg 异常信息：",e.getMessage());
        hashMap.put("data",null);
        return hashMap;
    }
}
